package utils

import (
	"AiBus/global"
	"AiBus/request"
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
)

func Arguments(req request.ArgumentsReq) []byte {

	url := "https://oa.api2d.net/v1/chat/completions"
	client := http.Client{}
	var buf bytes.Buffer
	err := json.NewEncoder(&buf).Encode(req)

	request, err := http.NewRequest("POST", url, &buf)
	request.Header.Add("Authorization", "Bearer fk218556-BBbVlG2LjANOWumfBNkuumtUKNWNlVju")
	request.Header.Add("Content-Type", "application/json")
	do, err := client.Do(request)
	if err != nil {
		global.App.Log.Error(err.Error())

	}

	all, err := io.ReadAll(do.Body)
	if err != nil {
		global.App.Log.Error(err.Error())

	}
	if err != nil {
		global.App.Log.Error(err.Error())

	}

	return all
}

func Credit_grants() request.Greditgrants {
	var grants request.Greditgrants
	url := "https://oa.api2d.net/dashboard/billing/credit_grants"
	method := "GET"

	client := http.Client{}
	req, err := http.NewRequest(method, url, nil)

	if err != nil {
		fmt.Println(err)
	}
	req.Header.Add("Authorization", "Bearer fk218556-BBbVlG2LjANOWumfBNkuumtUKNWNlVju")
	req.Header.Add("Content-Type", "application/json")

	res, err := client.Do(req)
	if err != nil {
		fmt.Println(err)

	}
	defer res.Body.Close()

	body, err := io.ReadAll(res.Body)
	if err != nil {
		global.App.Log.Error(err.Error())

	} else {
		err = json.Unmarshal(body, &grants)
		if err != nil {
			global.App.Log.Error(err.Error())

		} else {
			return grants
		}
	}
	return grants
}
